//--------------------Sonic CD HUD Script---------------------//
//--------Scripted by Christian Whitehead 'The Taxman'--------//
//-------Unpacked By Rubberduckycooly's Script Unpacker-------//

// Aliases
// Not all of these are used here in the HUD script, some are just used as a way for other objects to globally transmit values
private alias Object.Value0 : Object.PowerTimer     // Initially set via a loop in the UFO script
private alias Object.Value1 : Object.TimeSeconds   // Displayed time
private alias Object.Value2 : Object.TimeFrames    // Hidden and used for time processing, this value is managed by Special Setup instead of here
private alias Object.Value3 : Object.Rings
private alias Object.Value4 : Object.LastUFOType   // Used by UFOs for ring streak bonus
private alias Object.Value5 : Object.MainTimer    // Used to transmit between player and UFO
private alias Object.Value6 : Object.WinTimer    // "No" akin to Entity No, not as in "is there no UFO?"
private alias Object.Value7 : Object.winfade    // Used in making time deplete faster when wet
private alias Object.Value8 : Object.WinSlide    // Used in making time deplete faster when wet

private alias Object.Value9 : Object.PhoneShake    // Used in making time deplete faster when wet
private alias Object.Value10 : Object.PhoneTimer    // Used in making time deplete faster when wet


private alias Object[16].Value3 : Object.PuppetWind

private alias Object.Value23 : Object.Flash
private alias Object.Value24 : Object.TableContainer
private alias Object.Value25 : Object.ScareLeave

private alias Object.Value26 : Object.DogState
private alias Object.Value27 : Object.DogTimer
private alias Object.Value28 : Object.DogRand
private alias Object.Value29 : Object.FunEvent

private alias Object.Value30 : Object.MaxModeBeaten

// Game Mode Aliases
public value OfficePower = 100;
public value CameraEnabled = 0;


private alias object[10].value4 : Freddy.AI
private alias object[11].value4 : Bonnie.AI
private alias object[12].value4 : Chica.AI
private alias object[13].value4 : Foxy.AI
private alias object[14].value4 : Endo.AI
private alias object[15].value4 : GoldenFreddy.AI
private alias object[16].value4 : Puppet.AI
private alias object[17].value4 : Springtrap.AI

private alias object[18].value4 : WitheredFreddy.AI
private alias object[19].value4 : WitheredBonnie.AI
private alias object[20].value4 : WitheredChica.AI
private alias object[21].value4 : WitheredFoxy.AI

private alias object[22].value4 : BalloonBoy.AI
private alias object[23].value4 : JJ.AI

private alias object[24].value4 : ToyFreddy.AI
private alias object[25].value4 : ToyBonnie.AI
private alias object[26].value4 : ToyChica.AI
private alias object[27].value4 : Mangle.AI

private alias object[28].value4 : ShadowFreddy.AI
private alias object[29].value4 : ShadowBonnie.AI


// Priority
private alias 1 : PRIORITY_ACTIVE

reserve function RealHUD

function RealHUD

	temp1 = Screen.xsize
	temp1 -= 60
	
	DrawSpriteScreenXY(15, temp1, 25)	//NIGHT
	temp1 += 44
	DrawNumbers(0, temp1, 23, stage.actnum, 3, 8, 0)	//number

	temp1 -= 8
	DrawSpriteScreenXY(16, temp1, 10)	//TIME
	temp1 -= 12


	DrawSpriteScreenXY(22, temp1, 10)	//TIME


	temp1 -= 24
	if stage.minutes == 0
		DrawNumbers(0, temp1, 8, 12, 3, 8, 0)	//number
	else
		DrawNumbers(0, temp1, 8, stage.minutes, 3, 8, 0)	//number
	end if

	temp1 += 24
	if stage.seconds < 10
		temp1 -= 8
		DrawNumbers(0, temp1, 8, 0, 3, 8, 0)	//number
	
		temp1 += 8
		DrawNumbers(0, temp1, 8, stage.seconds, 3, 8, 0)	//number
	else
		DrawNumbers(0, temp1, 8, stage.seconds, 3, 8, 0)	//number	
	end if


	
	DrawSpriteScreenXY(17, 12, 178)	//POWER, USAGE
	temp0 = OfficePower
	temp0 /= 1
	DrawNumbers(0, 74, 176, temp0, 3, 8, 0)	//power %

	//every good coding casteor
	if PowerUsage >= 1
		DrawRect(54, 195, 11, 11, color2, color5, 0, 255)// 1
	end if	
	if PowerUsage >= 2
		DrawRect(67, 195, 11, 11, color2, color5, 0, 255)//2
	end if
	if PowerUsage >= 3
		DrawRect(80, 195, 11, 11, color7, color7, 0, 255)//2
	end if	
	if PowerUsage >= 4
		DrawRect(93, 195, 11, 11, color7, 0, 0, 255)//2
	end if	
	if PowerUsage >= 5
		DrawRect(106, 195, 11, 11, color7, 0, 0, 255)//2
	end if

	if Object.PuppetWind < 30

	end if

	temp4 = screen.xsize
	temp4 -= screen.xcenter
	temp4 -= 150
	DrawSpriteScreenXY(27, temp4, 212)	//BAR			

end function

event ObjectMain
switch object.state
case 0
	if Object[5].Type == TypeName[Blank Object]
		Object.TimeFrames++
		if Object.TimeFrames >= 60
			stage.seconds++
			if stage.seconds > 59
				stage.seconds = 0
				stage.minutes++
			end if
			Object.TimeFrames = 0
		end if	

		Object.PowerTimer++
		if Object.PowerTimer == 600
			OfficePower--
			Object.PowerTimer = 0
		end if
	
	end if


	if stage.minutes == 6
		Object[10].Type = TypeName[Blank Object]	
		Object[11].Type = TypeName[Blank Object]
		Object[12].Type = TypeName[Blank Object]
		Object[13].Type = TypeName[Blank Object]
		Object[14].Type = TypeName[Blank Object]
		Object[15].Type = TypeName[Blank Object]
		Object[16].Type = TypeName[Blank Object]
		Object[17].Type = TypeName[Blank Object]
		Object[18].Type = TypeName[Blank Object]
		Object[19].Type = TypeName[Blank Object]
		Object[20].Type = TypeName[Blank Object]
		Object[21].Type = TypeName[Blank Object]
		Object[22].Type = TypeName[Blank Object]
		Object[23].Type = TypeName[Blank Object]
		Object[24].Type = TypeName[Blank Object]

		Object[25].Type = TypeName[Blank Object]
		Object[26].Type = TypeName[Blank Object]
		Object[27].Type = TypeName[Blank Object]
		Object[28].Type = TypeName[Blank Object]		


		StopSfx(SfxName[Freddy1])	
		StopSfx(SfxName[Freddy2])	
		StopSfx(SfxName[Freddy3])	
		StopSfx(SfxName[Scream2])
		StopSfx(SfxName[Stare])
		StopSfx(SfxName[Fear])
		StopSfx(SfxName[Hum])
		StopSfx(SfxName[Fan])
		StopSfx(SfxName[Pots])
		StopSfx(SfxName[Jumpscare])
		StopSfx(SfxName[Fan2])
		StopSfx(SfxName[Fan3])
		StopSfx(SfxName[Breathing])
		StopSfx(SfxName[SpringScream])
		StopSfx(SfxName[PowerOut])
		NightWon = 1
		if stage.actnum == 7
			CheckEqual(Freddy.AI, 20)
			temp2 = checkResult
			CheckEqual(Bonnie.AI, 20)
			temp2 &= checkResult
			CheckEqual(Chica.AI, 20)
			temp2 &= checkResult
			CheckEqual(Foxy.AI, 20)
			temp2 &= checkResult
			CheckEqual(Endo.AI, 20)
			temp2 &= checkResult
			CheckEqual(GoldenFreddy.AI, 20)
			temp2 &= checkResult
			CheckEqual(Puppet.AI, 20)
			temp2 &= checkResult
			CheckEqual(Springtrap.AI, 20)
			temp2 &= checkResult
			CheckEqual(WitheredFreddy.AI, 20)
			temp2 &= checkResult
			CheckEqual(WitheredBonnie.AI, 20)
			temp2 &= checkResult
			CheckEqual(WitheredChica.AI, 20)
			temp2 &= checkResult												
			CheckEqual(WitheredFoxy.AI, 20)
			temp2 &= checkResult
			CheckEqual(BalloonBoy.AI, 20)
			temp2 &= checkResult
			CheckEqual(JJ.AI, 20)
			temp2 &= checkResult
			CheckEqual(ToyFreddy.AI, 20)
			temp2 &= checkResult
			CheckEqual(ToyBonnie.AI, 20)
			temp2 &= checkResult
			CheckEqual(ToyChica.AI, 20)
			temp2 &= checkResult
			CheckEqual(Mangle.AI, 20)
			temp2 &= checkResult		
			CheckEqual(ShadowFreddy.AI, 20)
			temp2 &= checkResult					
			CheckEqual(ShadowBonnie.AI, 20)
			temp2 &= checkResult				
			if temp2 == 1
				PlayMusic(6)
				Object.MaxModeBeaten = 1
			else
				PlayMusic(2)
			end if
		else
			if stage.actnum == 5
				PlayMusic(6)
			else
				PlayMusic(2)
			end if	
		end if
		music.volume = 100
		object.state++
	end if
break
case 1

	Object.WinSlide = 77


	if object.winfade < 256
		object.winfade += 8
	else
		object.winfade = 255
		object.state++
		object.inkeffect = 2
	end if	
break
case 2
	object.alpha += 8
	Object.MainTimer++
	if Object.MainTimer >= 50
		if Object.WinSlide < 110
			object.WinTimer++
			if object.WinTimer == 5
				Object.WinSlide += 1
				object.WinTimer = 0
			endif	
		else
			Object.WinSlide = 110
			if object.MainTimer > 500
				object.state++
			end if
		end if
	end if
break
case 3
	NightWon = 0

	LeftDoorClosed = 0
	RightDoorClosed = 0
	MiddleDoorClosed = 0
	UseMouse = 0
	OfficePower = 100
	CameraEnabled = 0
	PowerUsage = 1
	MaskMode = 0
	MaskEquipped = 0
	NightWon = 0

	LDoorLocked= 0
	RDoorLocked = 0
	MDoorLocked = 0

	LWindowScare= 0
	RWindowScare = 0

	PlayerJumpscared = 0
	ForcePull = 0
	OfficeScary = 0

	FlashlightOn = 0
	FlashLocked = 0

	if stage.actnum < 5
		stage.listPos++

		if options.gameMode == 1
			saveRAM[6000]++
			WriteSaveRAM()
		end if
	else

		if options.gameMode == 1
			//beat night 5
			saveRAM[6001] = 1

			//beat night 6
			if stage.actnum == 6	
				saveRAM[6002] = 1
			end if

			if Object.MaxModeBeaten == 1
				saveRAM[6003] = 1
			end if

		end if


		WriteSaveRAM()

		stage.activeList = BONUS_STAGE
		stage.listPos = 3

	end if	
	LoadStage()		
break
end switch	

if Object[5].Type == TypeName[Blank Object]
	if object.state == 0
		if stage.actnum == 2
			if OfficePower > 0
				switch Object.DogState
				case 0
					if CameraEnabled == 0
						Object.DogTimer++
						if Object.DogTimer == 2
							rand(Object.DogRand, 8000)
							rand(Object.FunEvent, 8000)
							if Object.FunEvent == Object.DogRand
							//	if inputpress.buttonZ == 1
									PlayMusic(5)
									Object.DogState++
							//	end if	
							end if	
							Object.DogTimer = 0
						end if
					end if	
				break
				case 1
					Object.DogTimer++
					temp0 = 2000
					rand(temp1, 500)
					temp0 += temp1
					if Object.DogTimer >= temp0
						PlayMusic(0)
						music.volume = 50
						object.DogState = 2
					end if
				break
				case 2
					Object.DogTimer = 0
				break
				end switch
			else
				Object.DogState = 0
			end if
		end if	
	end if	
end if	

end event

event ObjectDraw
	// Find the initial X Position to draw the HUD elements

	if OfficePower > 0


	else
		DrawRect(0, 0, screen.xsize, screen.ysize, 0, 0, 0, 224)// 1	
	end if


	if object[6].state != 2
		switch OfficeScary
		case 1
			object.flash++
			GetTableValue(temp2, object.TableContainer, OfficeFlash)
			if object.flash >= 12
				object.TableContainer++
				if object.TableContainer > 3
					object.TableContainer = 0
				end if	
				object.flash = 0
			end if			
			
			DrawRect(0, 0, screen.xsize, screen.ysize, 0, 0, 0, temp2)// 1
		break
		case 2
			if Object.ScareLeave > 0
				Object.ScareLeave -= 6
			end if	
			DrawRect(0, 0, screen.xsize, screen.ysize, 0, 0, 0, Object.ScareLeave)// 1
		break
		case 3

			object.flash++
			GetTableValue(temp2, object.TableContainer, OfficeFlash)
			if object.flash >= 12
				object.TableContainer++
				if object.TableContainer > 3
					object.TableContainer = 0
				end if	
				object.flash = 0
			end if			
			
			DrawRect(0, 0, screen.xsize, screen.ysize, 0, 0, 0, temp2)// 1

			if Object.ScareLeave > 0
				Object.ScareLeave -= 6
			end if	
			DrawRect(0, 0, screen.xsize, screen.ysize, 0, 0, 0, Object.ScareLeave)// 1
		break		
		end switch
	end if

	if OfficePower > 0
		CallFunction(RealHUD)
	end if


	switch Object.DogState
	case 1
		//dog
		object.PhoneTimer++
		if object.PhoneTimer >= 50
			rand(Object.PhoneShake, 8)
			if object.PhoneTimer >= 100
				object.PhoneTimer = 0
			end if
		end if

		DrawSpriteScreenFX(28, FX_ROTATE, Object.PhoneShake, 36)	//phone
	break
	end switch


	//mouse
	DrawSpriteScreenXY(19, touchscreen[0].xpos, touchscreen[0].ypos)

	if Object.PuppetWind < 30
		temp2 = screen.xcenter
		temp2 += 175
		DrawSpriteScreenXY(23, temp2, 190)	//AM
	end if

	if Object.PuppetWind < 10
		temp2 = screen.xcenter
		temp2 += 175
		DrawSpriteScreenXY(24, temp2, 190)	//AM
	end if

	if object[30].type != TypeName[Blank Object]
		DrawSpriteScreenXY(25, 20, 0)	//NIGHT		
	end if
	


	//winning states
	switch object.state
	case 1
		DrawRect(0, 0, screen.xsize, screen.ysize, 0, 0, 0, object.winfade)// 1
	break
	case 2
	case 3
		
		DrawRect(0, 0, screen.xsize, screen.ysize, 0, 0, 0, object.winfade)// 1


		temp0 = screen.xcenter
		temp0 -= 48
		DrawSpriteScreenFX(20, FX_INK, temp0, 110)	//AM

		
		
		DrawSpriteScreenFX(21, FX_INK, temp0, Object.WinSlide)	//AM

		DrawRect(0, 0, screen.xsize, 100, 0, 0, 0, object.winfade)// 1
		DrawRect(0, 140, screen.xsize, 100, 0, 0, 0, object.winfade)// 1'


	break
	end switch


//	DrawNumbers(0, 32, 96, Object.FunEvent, 3, 8, 0)

end event


event ObjectStartup
	
	LoadSpriteSheet("FNAF/Objects.gif")
	
	// Place the HUD object into reserved object slot 4 and initialise its values
	Object[4].Type = TypeName[HUD]
	
	// The object should always be run, regardless if it's within camera bounds or not
	Object[4].Priority = PRIORITY_ACTIVE
	
	// Make the HUD draw ontop of all other sprites, too
	Object[4].DrawOrder = 6
	
	// Reset the last cleared UFO type
	// (Needs to be set to -1 and not just default 0 because the Rings UFO uses type 0)
	Object[4].LastUFOType = -1
	
	// 0-9 - Numbers 0-9
	SpriteFrame(0, 0, 8, 13, 558, 1)
	SpriteFrame(0, 0, 8, 13, 567, 1)
	SpriteFrame(0, 0, 8, 13, 576, 1)
	SpriteFrame(0, 0, 8, 13, 585, 1)
	SpriteFrame(0, 0, 8, 13, 594, 1)
	SpriteFrame(0, 0, 8, 13, 558, 15)
	SpriteFrame(0, 0, 8, 13, 567, 15)
	SpriteFrame(0, 0, 8, 13, 576, 15)
	SpriteFrame(0, 0, 8, 13, 585, 15)
	SpriteFrame(0, 0, 8, 13, 594, 15)
	
	// 10 - UFO Counter Graphic
	SpriteFrame(0, 0, 24, 16, 206, 58)
	
	// 11 - Time Graphic
	SpriteFrame(0, 0, 24, 8, 231, 58)
	
	// 12 - Rings Graphic
	SpriteFrame(0, 0, 31, 8, 206, 75)
	
	// 13 - Single Tick Graphic
	SpriteFrame(0, 0, 8, 13, 224, 112)
	
	// 14 - Double Tick Graphic
	SpriteFrame(0, 0, 8, 13, 233, 112)

	// 15 - Night
	SpriteFrame(0, 0, 36, 11, 753, 558)	
	
	// 16 - :AM
	SpriteFrame(0, 0, 20, 11, 790, 558)		

	// 17 - POWER, USAGE
	SpriteFrame(0, 0, 40, 28, 753, 614)	

	// 18 - Bar
	SpriteFrame(0, 0, 299, 23, 1, 936)	

	// 19 - mice
	SpriteFrame(0, 0, 12, 21, 1011, 1)

	// 20 - Bar
	SpriteFrame(0, 0, 88, 24, 850, 1)	

	//21
	SpriteFrame(0, 0, 54, 57, 850, 26)	

	//22
	SpriteFrame(-13, 4, 3, 7, 794, 614)		

	//23
	SpriteFrame(-13, 4, 32, 32, 603, 85)		
	SpriteFrame(-13, 4, 32, 32, 603, 52)		//24
	//mute call, not nice tho 		25
	SpriteFrame(-13, 4, 88, 24, 761, 1)				

	SpriteFrame(-128,-128,256,256,1,252)	//35

	// 27 - Bar
	SpriteFrame(0, 0, 300, 23, 1, 221)		

	//28 - Dog is Calling
	SpriteFrame(4, 0, 56, 92, 661, 101)			
end event


// ========================
// Editor events
// ========================

event RSDKDraw
	DrawSprite(0)
end event


event RSDKLoad
	LoadSpriteSheet("3D_Test/Objects.gif")
	SpriteFrame(0, 0, 24, 16, 206, 58)

	SetVariableAlias(ALIAS_VAR_PROPVAL, "unused")
end event
